package com.xiayong.datahandler.impl;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.xiayong.datahandler.ICityDataHandler;
import com.xiayong.dataprovider.ICityDataProvider;
import com.xiayong.dto.Pager;
import com.xiayong.entity.City;

@Service
public class CityDataHandlerImpl implements ICityDataHandler {
    @Autowired
    private ICityDataProvider cityDataProvider;

    @Override
    public List<City> getCityByPager(final Pager pager) {
        Map<String, Serializable> parameterMap = new HashMap<String, Serializable>();
        if (StringUtils.isNotBlank(pager.getQtype())) {
            parameterMap.put("qtype", pager.getQtype());
            parameterMap.put("query", pager.getQuery());
        }
        parameterMap.put("startrow", (pager.getPage() - 1) * pager.getRp());
        parameterMap.put("pagesize", pager.getRp());
        parameterMap.put("sortname", pager.getSortname());
        parameterMap.put("sortorder", pager.getSortorder());
        return this.cityDataProvider.findCity(parameterMap);
    }

    @Override
    public int getCityTotal(final Pager pager) {
        Map<String, Serializable> parameterMap = new HashMap<String, Serializable>();
        if (pager != null) {
            if (StringUtils.isNotBlank(pager.getQtype())) {
                parameterMap.put("qtype", pager.getQtype());
                parameterMap.put("query", pager.getQuery());
            }
            parameterMap.put("startrow", (pager.getPage() - 1) * pager.getRp());
            parameterMap.put("pagesize", pager.getRp());
            parameterMap.put("sortname", pager.getSortname());
            parameterMap.put("sortorder", pager.getSortorder());
        }
        return this.cityDataProvider.getTotal(parameterMap);
    }

}
